package com.ruoyi.web.controller.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.controller.demo.domain.SysWarehouse;
import com.ruoyi.web.controller.mapper.InstrumentListMapper;
import com.ruoyi.web.controller.demo.domain.InstrumentList;
import com.ruoyi.web.controller.service.IInstrumentListService;
import com.ruoyi.common.core.text.Convert;

/**
 * 仪器设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-02
 */
@Service
public class InstrumentListServiceImpl implements IInstrumentListService 
{
    @Autowired
    private InstrumentListMapper instrumentListMapper;

    /**
     * 查询仪器设备
     * 
     * @param instrumentId 仪器设备主键
     * @return 仪器设备
     */
    @Override
    public InstrumentList selectInstrumentListByInstrumentId(Long instrumentId)
    {
        return instrumentListMapper.selectInstrumentListByInstrumentId(instrumentId);
    }

    /**
     * 查询仪器设备列表
     * 
     * @param instrumentList 仪器设备
     * @return 仪器设备
     */
    @Override
    public List<InstrumentList> selectInstrumentListList(InstrumentList instrumentList)
    {
        return instrumentListMapper.selectInstrumentListList(instrumentList);
    }

    /**
     * 新增仪器设备
     * 
     * @param instrumentList 仪器设备
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInstrumentList(InstrumentList instrumentList)
    {
        int rows = instrumentListMapper.insertInstrumentList(instrumentList);
        insertSysWarehouse(instrumentList);
        return rows;
    }

    /**
     * 修改仪器设备
     * 
     * @param instrumentList 仪器设备
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInstrumentList(InstrumentList instrumentList)
    {
//        instrumentListMapper.deleteSysWarehouseById(instrumentList.getInstrumentId());
        insertSysWarehouse(instrumentList);
        return instrumentListMapper.updateInstrumentList(instrumentList);
    }

    /**
     * 批量删除仪器设备
     * 
     * @param instrumentIds 需要删除的仪器设备主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInstrumentListByInstrumentIds(String instrumentIds)
    {
//        instrumentListMapper.deleteSysWarehouseByIds(Convert.toStrArray(instrumentIds));
        return instrumentListMapper.deleteInstrumentListByInstrumentIds(Convert.toStrArray(instrumentIds));
    }

    /**
     * 删除仪器设备信息
     * 
     * @param instrumentId 仪器设备主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInstrumentListByInstrumentId(Long instrumentId)
    {
//        instrumentListMapper.deleteSysWarehouseById(instrumentId);
        return instrumentListMapper.deleteInstrumentListByInstrumentId(instrumentId);
    }

    /**
     * 新增sys_warehouse信息
     * 
     * @param instrumentList 仪器设备对象
     */
    public void insertSysWarehouse(InstrumentList instrumentList)
    {
        List<SysWarehouse> sysWarehouseList = instrumentList.getSysWarehouseList();
        Long instrumentId = instrumentList.getInstrumentId();
        if (StringUtils.isNotNull(sysWarehouseList))
        {
            List<SysWarehouse> list = new ArrayList<SysWarehouse>();
            for (SysWarehouse sysWarehouse : sysWarehouseList)
            {
                sysWarehouse.setId(instrumentId);
                list.add(sysWarehouse);
            }
            if (list.size() > 0)
            {
                instrumentListMapper.batchSysWarehouse(list);
            }
        }
    }
}
